﻿using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
using ZCommon;
using ZDriver;

namespace Launch.ViewModel;

public partial class SerialPortSettingVm : ObservableObject
{
    // 串口驱动
    private readonly SerialPort_Driver _serialPortDriver = Singleton<SerialPort_Driver>.Instance();
    
    // 当前连接状态
    [ObservableProperty] private string _connectState = "未连接";

    // 波特率
    [ObservableProperty] private string _baudRate = "115200";

    // 数据位
    [ObservableProperty] private string _dataBits = "8";

    // 校验位
    [ObservableProperty] private string _parity = "Even";

    // 端口列表
    [ObservableProperty] private List<string> _portNames = new();

    // 选中的端口
    [ObservableProperty] private string _selectedPortName = "";

    // 停止位
    [ObservableProperty] private string _stopBits = "1";
    
    // 节点接收数据状态
    [ObservableProperty] private string _nodeReceiveBytesStatus = "";
    
    // 目标节点ID
    [ObservableProperty] private ushort _nodeId = 2288;
    
    // 发送间隔时间ms
    [ObservableProperty] private int _sendIntervalMs = 50;
    
    // 给目标节点发送的数据
    [ObservableProperty] private string _sendDataToNode = "";
    
    // 已发送字节数
    [ObservableProperty] private uint _sendBytesCount = 0;
    
    // 已发送帧数
    [ObservableProperty] private uint _sendFramesCount = 0;
    
    // 串口已接收数据
    [ObservableProperty] private string _serialPortReceivedData = "";

    public SerialPortSettingVm()
    {
        // 获取当前所有的串口名称
        var ports = SerialPort_Driver.GetPortNames();
        foreach (var port in ports) PortNames.Add(port);

        // 默认选择第一个端口
        if (PortNames.Count > 0) SelectedPortName = PortNames[0];
    }
}